package Man.glass.bean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import Man.glass.entity.manager;
import Man.glass.mgr.AppManagerMgr;

@ManagedBean(name="appLoginManagerBean")
@SessionScoped
public class AppLoginManagerBean {
	
	private String mgrName;
	private String mgrPass;
	
	AppManagerMgr AMM =new AppManagerMgr();
	
	public void initPage(){
		mgrName="";
		mgrPass="";
	}
	
	public AppLoginManagerBean(){
		mgrName="";
		mgrPass="";
	}
	
	public String checkLogin(){
		if(mgrName!="" && mgrPass!=""){
			List<manager> managerList = AMM.queryManager();
			boolean loginOK=false;
			for(manager m : managerList){
				if(m.getName().equals(mgrName)&& m.getPassword().equals(mgrPass)){
					loginOK=true;
				}
			}
			if(loginOK){
				return "/page/indexManager";
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The administrator is not exist!")); 
				return null;
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The input box can not be empty!")); 
			return null;
		}
	}

	
	public String goToManagerLogin(){
		initPage();
		return "/page/loginManager";
	}

	public String getMgrName() {
		return mgrName;
	}

	public void setMgrName(String mgrName) {
		this.mgrName = mgrName;
	}

	public String getMgrPass() {
		return mgrPass;
	}

	public void setMgrPass(String mgrPass) {
		this.mgrPass = mgrPass;
	}
	
}
